package com.audaque.springboot.foshanupload.web.mbgen.gen.action;

import cn.hutool.core.bean.BeanUtil;
import com.audaque.springboot.foshanupload.core.util.ResourceUtil;
import com.audaque.springboot.foshanupload.core.util.StringUtils;
import com.audaque.springboot.foshanupload.web.mbgen.gen.api.RunGenApi;
import com.audaque.springboot.foshanupload.web.mbgen.gen.model.req.RunGenReq;
import com.audaque.springboot.foshanupload.web.mbgen.gen.properties.MbgenProperties;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.TableConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zgb
 * @desc ...
 * @date 2023-01-26 20:53:51
 */
@Slf4j
@Controller
public class RunGenAction implements RunGenApi {

    @Autowired
    private MbgenProperties mbgenProperties;


    public Boolean generate(@RequestBody RunGenReq runGenReq) throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //如果这里出现空指针，直接写绝对路径即可。
        String genCfg = mbgenProperties.getMbgenXmlClassPath();
        String pathByHutoolResourceUtil = ResourceUtil.getPathByHutoolResourceUtil(genCfg);
        File configFile = ResourceUtils.getFile(pathByHutoolResourceUtil);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = null;
        try {
            config = cp.parseConfiguration(configFile);
            List<Context> contexts = config.getContexts();
            Context context = contexts.get(0);
            List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
            if (runGenReq != null) {
                TableConfiguration tableConfiguration = tableConfigurations.get(0);
                String tableName = runGenReq.getTableName();
                if(StringUtils.isNotBlank(tableName)){
                    tableConfiguration.setTableName(tableName);
                }

                if(StringUtils.isNotBlank(tableName)){
                    tableConfiguration.setDomainObjectName(StringUtils.toCapitalizeCamelCase(tableName));
                }
                String[] tableNameList = runGenReq.getTableNameList();
                if(tableNameList.length>0){
                    List<String> list = new ArrayList<>(Arrays.asList(tableNameList));
                    List<TableConfiguration> collect = list.stream().map(e -> {
                        TableConfiguration tableConfiguration1 = new TableConfiguration(context);
                        BeanUtil.copyProperties(tableConfiguration, tableConfiguration1);
                        tableConfiguration1.setTableName(e);
                        tableConfiguration1.setDomainObjectName(StringUtils.toCapitalizeCamelCase(e));
                        return tableConfiguration1;
                    }).collect(Collectors.toList());
                    tableConfigurations.clear();
                    tableConfigurations.addAll(collect);

                }

            }


            log.debug("config:" + config);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMLParserException e) {
            e.printStackTrace();
        }


        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = null;
        try {
            myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        try {
            myBatisGenerator.generate(null);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }
}
